﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;

namespace YehbaLogin
{
    public partial class Yehba : Form
    {
        #region Controls and Variables
        
        //has something to do with easy drag and drop of form to any location even without form title bar
        private bool IsFormBeingDragged = false;
        private int MouseDownX;
        private int MouseDownY;
        
        //call web service methods here
        localhost.ybWebSrvc myService = new YehbaLogin.localhost.ybWebSrvc();
        //call Message class when message arrived
        YehbaLogin.localhost.Message msg = new YehbaLogin.localhost.Message();

        //create an instance of FormsCollection
        public static FormsCollection Forms = new FormsCollection();
        
        //delegates
        public delegate void Showing();
        public delegate void ListBoxUpdate(String str);

        //other variables
        public static String name = " ";

        #endregion

        #region Constructor
        public Yehba()
        {
           
             InitializeComponent();

        }
        #endregion

        #region Form and Control Events

        //upon yehba load
        private void Yehba_Load(object sender, EventArgs e)
        {

            this.webBrowser1.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.webBrowser1_DocumentCompleted);

            //create region with rounded corners
            Point[] myArray =
             {
                 new Point(1, 6),
                 new Point(3, 3),
                 new Point(7, 0),
                 new Point(293,0),
                 new Point(299,9),
                 new Point(299,9),
                 new Point(299,179),
                 new Point(295,184),
                 new Point(291,187),
                 new Point(8,187),
                 new Point(3,184),
                 new Point(0,180),
             };

            Point[] myArray2 =
             {
                 new Point(15, 44),
                 new Point(17, 40),
                 new Point(22, 38),
                 new Point(276,38),
                 new Point(281,41),
                 new Point(283,45),
                 new Point(283,150),
                 new Point(279,153),
                 new Point(276,155),
                 new Point(22,155),
                 new Point(18,153),
                 new Point(15,149),
                 
             };
  
            NewRegion form_region = new NewRegion(myArray);
            NewRegion panel_region = new NewRegion(myArray2);
            //create new form region
            this.Region = new Region(form_region.getGraphicsPath);
            //create new panel region
            this.panel1.Region = new Region(panel_region.getGraphicsPath);
        }


        //user moved this form up
        private void Yehba_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                IsFormBeingDragged = false;
            }
        }

        //user moved this form down
        private void Yehba_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                IsFormBeingDragged = true;
                MouseDownX = e.X;
                MouseDownY = e.Y;
            }
        }

        //user moves this form(get the coordinates)
        private void Yehba_MouseMove(object sender, MouseEventArgs e)
        {
            if (IsFormBeingDragged)
            {
                Point temp=new Point();
                temp.X = this.Location.X + (e.X - MouseDownX);
                temp.Y = this.Location.Y + (e.Y - MouseDownY);
                this.Location = temp;
                temp.X = 0;
                temp.Y = 0;

            }
        }

        //user closes this form
        private void btnClose_Click(object sender, EventArgs e)
        {
            try
            {
                this.BackColor = Color.LightBlue;
                this.BackgroundImage = null;
                panel1.Controls.Remove(webBrowser1);
                panel1.BackColor = Color.LightBlue;
                WinAPI.AnimateWindow(this.Handle, 1000, 0X10000 | 0X80000);
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "--" + ex.StackTrace.ToString());
            }
        }

        //user clicks maximize button
        private void btnMaximize_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
        }

        //user clicks minimize button
        private void btnMinimize_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        //web browser event, when the browser has started navigating
        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            //once url changed to SignedIn.htm this form will hide
           if (e.Url.Equals("http://localhost:2640/SignedIn.htm"))
             {
                this.BackColor = Color.LightBlue;
                this.BackgroundImage = null;

                panel1.Controls.Remove(webBrowser1);
                panel1.BackColor = Color.LightBlue;
                //window animation(fading)
                WinAPI.AnimateWindow(this.Handle, 1000, 0X10000 | 0X80000);
                this.Hide();
       
            }
        }

        //will use web service methods
        //these web methods are called to display separate window like chat window 
        //use timer control to listen to incoming messages every minute(instead of creating seperate thread)
        private void timer1_Tick(object sender, EventArgs e)
        {
            //call web service method getMessageAsync() (for message to be displayed on chat window)
            myService.getMessageAsync();

            //call web service getChatBoxName() (in order to create or find the chat window to serve new messages)
            name = myService.getChatBoxName();
            
            //call web service then delete the chat box name
            myService.clearChatBoxName();

            //if name is given or retrieved
            if (name != String.Empty)
            {
                //inform ChatBox about the name of the new ChatBox window
                ChatBox.name = name;
                //create an instance of ChatBox
                ChatBox chat = new ChatBox();

                //check if this form is already added in FormsCollection
                if (ChatBox.flag == true)
                {
                    //if yes, show it
                    chat.CreateChatBox();
                    chat.Name = name;
                    chat.Text = name;
                    chat.User.Text = name;
                    chat.Show();
                }
                else
                {
                    //if flag is false, means uncertain, then you have to search it in FormsCollection
                    //get the static variable that returns true if form is found in the collection, otherwise false
                    if (ChatBox.found == true)
                    {
                        //if form is found, loop in FormsCollection
                        foreach (Form LoopForm in Forms)
                        {
                            //check the names of form in FormsCollection
                            if (LoopForm.Name == name)
                            {
                                //Check the Controls of a particular Form
                                foreach (System.Windows.Forms.Control LoopControl in LoopForm.Controls)
                                {
                                    //compare the controls to listBox1 where the message will be displayed
                                    if (LoopControl.Name.ToString() == "listBox1")
                                    {
                                        //if the control is listBox1, then add the message
                                        ListBox myListBox = (ListBox)LoopControl;
                                        //test if working
                                        myListBox.Items.Add("bla bla");
                                    }

                                }

                            }
                        }

                    }
                }

            }

        }
        #endregion

        #region web service callbacks 
        public void myService_getMessageCompleted(object sender, YehbaLogin.localhost.getMessageCompletedEventArgs args)
        {
            try
            {
                //message here originated from web service
                //if message is not empty
                if (args.Result != null)
                {
                    //get the result 
                    msg = args.Result;
                    //MessageBox.Show(msg.Body + msg.From); //test
                }
            }
            catch (Exception)
            {
                //MessageBox.Show(ex.InnerException.ToString() + " ------ " + ex.InnerException.StackTrace.ToString());
            }
        }
        #endregion

    }
}
